package com.lebang.controller.sys.other;

import com.alibaba.fastjson.JSONObject;
import com.lebang.aop.ConfigRecord;
import com.lebang.common.Login;
import com.lebang.domain.AppResoure;
import com.lebang.example.AppResoureExample;
import com.lebang.model.Whether;
import com.lebang.service.sys.AppLookCategoryService;
import com.lebang.service.sys.AppResoureOpenService;
import com.lebang.service.sys.other.AppResoureService;
import com.lebangweb.response.Callback;
import com.lebangweb.response.JsonData;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.util.List;


/**
 * 新菜单
 *
 * @author hwq
 * @date 2020-04-01
 */
@Controller
@RequestMapping("/sys/appresoure")
public class AppResoureController {
    @Autowired
    private AppResoureService baseService;
    @Autowired
    private AppLookCategoryService appLookCategoryService;
    @Autowired
    private AppResoureOpenService appResoureOpenService;

    @GetMapping("/list")
    public String list() {
        return "/appresoure/list";
    }

    @GetMapping("/add")
    public String add(Integer id, Model model) {
        AppResoure vo;
        if (id != null) {
            vo = baseService.selectById(id);
        } else {
            vo = new AppResoure();
        }
        model.addAttribute("item", vo);
        AppResoureExample example = new AppResoureExample();
        example.createCriteria().andParentIdEqualTo(0);
        List<AppResoure> list = baseService.selectByExample(example);
        model.addAttribute("list", list);
        model.addAttribute("looklist", appLookCategoryService.getAll());
        return "/appresoure/edit";
    }

    @PostMapping("/edit")
    @ResponseBody
    @ConfigRecord
    public JsonData edit(AppResoure vo) {
        if (vo.getId() != null) {
            vo.setModifyTime(LocalDateTime.now());
            baseService.updateById(vo);
        } else {
            vo.setCreateTime(LocalDateTime.now());
            vo.setHasDeleted(0);
            baseService.insertModel(vo);
        }
        baseService.deleteRedis();
        return Callback.success("success", "修改成功");
    }

    @Login
    @PostMapping("/delete")
    @ResponseBody
    public JsonData delete(Integer id) {
        baseService.deleteById(id);
        baseService.deleteRedis();
        return Callback.success("success", "删除成功");
    }

    @ResponseBody
    @ApiOperation(value = "分页")
    @PostMapping("/list")
    public JSONObject list(HttpServletRequest request, @RequestParam Integer page, @RequestParam Integer limit) {
        AppResoureExample example = new AppResoureExample();
        example.setOffset((page - 1) * limit.longValue());
        example.setLimit(limit);
        AppResoureExample.Criteria criteria = example.createCriteria();
        criteria.andParentIdNotEqualTo(0);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("code", 0);
        jsonObject.put("data", baseService.selectByExample(example));
        jsonObject.put("count", baseService.countByExample(example));
        return jsonObject;
    }

    @GetMapping("/parentlist")
    public String parent() {
        return "/appresoure/parent-list";
    }

    @GetMapping("/parentadd")
    public String parent(Integer id, Model model) {
        AppResoure vo;
        if (id != null) {
            vo = baseService.selectById(id);
        } else {
            vo = new AppResoure();
        }
        model.addAttribute("item", vo);
        model.addAttribute("list", appResoureOpenService.selectByExample(null));
        return "/appresoure/parent-edit";
    }

    @ResponseBody
    @ApiOperation(value = "分页")
    @PostMapping("/parentlist")
    public JSONObject parentlist(HttpServletRequest request, @RequestParam Integer page, @RequestParam Integer limit) {
        AppResoureExample example = new AppResoureExample();
        example.setOffset((page - 1) * limit.longValue());
        example.setLimit(limit);
        AppResoureExample.Criteria criteria = example.createCriteria();
        criteria.andParentIdEqualTo(0);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("code", 0);
        jsonObject.put("data", baseService.selectByExample(example));
        jsonObject.put("count", baseService.countByExample(example));
        return jsonObject;
    }

    @PostMapping("/enable")
    @ResponseBody
    @ConfigRecord
    public JsonData enable(@RequestBody AppResoure appResoure) {
        AppResoure vo = baseService.selectById(appResoure.getId());
        vo.setEnable(appResoure.getEnable());
        vo.setModifyTime(LocalDateTime.now());
        baseService.updateById(vo);
        baseService.deleteRedis();
        return Callback.success("success", "修改成功");
    }

    @Login
    @GetMapping("/app/add/record")
    public String record() {
        return "/appresoure/newapi-record";
    }

    @Login
    @PostMapping("/app/add/record")
    @ResponseBody
    public JsonData record(String taskId, String icon, String title, String type, String msg) {

        AppResoure main = new AppResoure();
        main.setNewhandCategory("99");
        main.setType(99);
        main.setHasDeleted(Whether.NO.getDbIndex());
        main.setCreateTime(LocalDateTime.now());
        main.setModifyTime(LocalDateTime.now());
        main.setTitle(title);
        main.setPicture(icon);
        main.setSort(99);
        main.setUrl("web/api/task/taskdetail?taskId=" + taskId+"&type=common");
        main.setMsg(msg);
        main.setEnable(Whether.YES.getDbIndex());
        main.setAppStore("all");
        main.setParentId(2);
        int i = baseService.insertModel(main);
        if (i > 0) {
            return Callback.success("success", "上架成功", null);
        } else {
            return Callback.fail("fail", "操作失败", null);
        }
    }
}
